<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠
// +----------------------------------------------------------------------
namespace app\edu\controller;

use app\admin\controller\Admin;
use app\admin\model\Office;
use app\edu\model\Teacher as TeacherModel;

/**
 * 教师控制器
 *
 * @author 李忠
 * @createAt 2018-11-08 14:49:00
 *
 */
class Teacher extends Admin {

    /**
     * 列表页面
     */
    public function index($status = null, $office_id = null, $type = null, $title = null) {
        cookie('__forward__', $this->request->url());

        // 数据列表
        $data_list = TeacherModel::lists($status, $office_id, $type, $title);
        $this->assign('data_list', $data_list);
        $this->assign('pager', $data_list->render());

        $this->assign('office_tree', list_to_config(Office::listMy()));

        $this->assign('status', $status);
        $this->assign('office_id', $office_id);
        $this->assign('type', $type);
        $this->assign('title', $title);
        $this->setMeta('教师管理');
        return $this->fetch();
    }

    // 新增教师
    public function add() {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Teacher');
            if (true !== $result) {
                $this->error($result);
            }
            if ($data = TeacherModel::create($data)) {
                $this->log($data['id'], '新增教师成功');
                $this->success('新增教师成功', cookie('__forward__'));
            } else {
                $this->error('新增教师失败');
            }
        } else {
            $this->assign('info', null);
            $this->setMeta('新增教师');
            return $this->fetch();
        }
    }

    // 更新教师
    public function edit($id = null) {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Teacher');
            if (true !== $result) {
                $this->error($result);
            }
            if ($data = TeacherModel::update($data)) {

                $this->log($data['id'], '更新教师成功');
                $this->success('更新教师成功', cookie('__forward__'));
            } else {
                $this->error('更新教师失败');
            }

        } else {
            $info = TeacherModel::get($id);

            $this->assign('info', $info);
            $this->setMeta('更新教师');
            return $this->fetch('add');
        }
    }

    /**
     * 禁用
     * @param int $id
     */
    public function forbid($id) {
        if (TeacherModel::state($id, -1)) {
            $this->log($id, '禁用教师成功');
            $this->success('禁用教师成功');
        } else {
            $this->error('禁用教师失败');
        }
    }

    /**
     * 恢复
     * @param int $id
     */
    public function resume($id) {
        if (TeacherModel::state($id, -1)) {
            $this->log($id, '启用教师成功');
            $this->success('启用教师成功');
        } else {
            $this->error('启用教师失败');
        }
    }

    /**
     * 删除
     * @param int $id
     */
    public function delete($id) {
        if (TeacherModel::state($id, -1)) {
            $this->log($id, '删除教师成功');
            $this->success('删除教师成功');
        } else {
            $this->error('删除教师失败');
        }
    }

}